var SysUser = {
	config : {},
	init : function(config) {
		var _this = this;
		$.extend(_this.config, config);
		Layout.set_menu_select(_this.config.menu_id);

    $('#search_form')[0].reset();
		$('.dtp').datetimepicker(DTP_DATE_OPTION);
		loading();
		_this.init_btn();
		_this.init_grid();
		sessionStorage.removeItem("scroll_top");
    Group.init({
      html_id : 'group_html',
      get_url : _this.config.get_group_url ,
    });
	},

	init_btn : function() {
		var _this = this;

		// 查询按钮
		$(document).on('click', '#search_btn', function(e) {
			e.preventDefault();
			var query_param = $('#data_table').TableGrid('getparam');
      query_param.keyword = $('#keyword').val();
			query_param.time_from = $('#time_from').val();
			query_param.time_to = $('#time_to').val();
			query_param.status = $('#status').val();
      query_param.role_id = $('#role_id').val();
			query_param.page = 1;

			$('#data_table').TableGrid('param', query_param);
			$('#data_table').TableGrid('reload');
		});

		// 打开添加窗口
		$(document).on('click', '.load_add_modal_btn', function(e) {
			e.preventDefault();
			_this.config.action = 'create';
			$('#submit_form').attr('action', _this.config.create_url);
			$('#submit_form')[0].reset();
			fillform($('#submit_form'), _this.config.row);
      Group.get( '' , _this.config.sys_type , 0 );
			$('#action_label').html('新增');
			$('#add_edit_modal').modal('show');
		});

		// 打开编辑窗口
		$(document).on('click', '.edit_btn', function(e) {
			e.preventDefault();
			var index = $('.edit_btn').index($(this));
			var data = $('#data_table').TableGrid('getrow', index);
      
      $('#submit_form')[0].reset();
			fillform($('#submit_form'), data);
      //设置角色
      var role_id = data.role_id.split(',');
      for( var i = 0 ; i < role_id.length ; i++ ) {
        $('input[name="role_id"][value="'+ role_id[i] +'"]').prop('checked' , true );
      }
      
      //设置用户组
      Group.get(data.group_id , _this.config.sys_type , 0 );

			$('#submit_form').attr('action', _this.config.update_url + '/' + data['id']);
			_this.config.action = 'update';
			$('#action_label').html('编辑');
			$('#add_edit_modal').modal('show');
		});

		// 提交
		$(document).on('click', '#submit_btn', function(e) {
			e.preventDefault();
			var _thisbtn = $(this);
			var url = $('#submit_form').attr('action');
			if (validform($('#submit_form'))) {
				var data = $('#submit_form').serializeObject();
        data.group_id = new Array();
        for( var i = 0 ; i < $('#group_html').find('.active').length ; i++ ) {
          data.group_id.push( $('#group_html').find('.active').eq(i).data('id') );
        }
        
        if ( data.group_id.length == 0 ) {
          tips('请选择角色');  
          return ;
        }
        
				$.post(url, data, function(ret) {
					tips(ret.info);
					if (ret.status) {
						$('#add_edit_modal').modal('hide');
						var query_param = $('#data_table').TableGrid('getparam');
						query_param.page = 1;
						$('#data_table').TableGrid('param', query_param);
						$('#data_table').TableGrid('reload');
					}
				});
			}
		});
    


		// 批量删除按钮
		$(document).on('click', '.del-btn', function(e) {
			e.preventDefault();
			var select_id = new Array();
			$("input[name='tablegrid-select[]']").each(function(index) {
				if ($(this).prop('checked')) {
					// 取表数据
					var row = $('#data_table').TableGrid('getrow', index);
					select_id.push(row.id);
				}
			});

			if (select_id.length == 0) {
				tips('请选择数据');
			} else {
				_this.config.del_id = select_id.join(',');
				_this.del_data();
			}
		});

		// 单个删除按钮
		$(document).on('click', '.del_one_btn', function(e) {
			e.preventDefault();
			var index = $('.del_one_btn').index($(this));
			var data = $('#data_table').TableGrid('getrow', index);
			_this.config.del_id = data['id'];
			_this.del_data();
		});
        
    //reset_pwd_btn
		$(document).on('click', '.reset_pwd_btn', function(e) {
			e.preventDefault();
			var id = $(this).data('id');
			var url = _this.config.reset_pwd_url + id ;
      sure.init('密码将重置为123456，确认？', function(){
        $.get( url , function(ret){
          tips(ret.info);
        });
      });
		});

	},

	init_grid : function() {
		var _this = this;
		$('#data_table').TableGrid({
			url : _this.config.read_url,
			select_all : false,
			load_after_init : true,
			row_style : function(row, index) {
				if (row.status != 7 ) {
					return 'info';
				}
			}
		});
	},

	del_data : function(ids) {
		var _this = this;

		sure.init('确定删除吗？', function() {
			var url = _this.config.delete_url;
			var data = {
				ids : _this.config.del_id
			};

			$.post(url, data, function(ret) {
				tips(ret.info);
				if (ret.status) {
					$('#data_table').TableGrid('reload');
				}
			});
		});
	},
};

var format_username = function(value, row, index) {
	return '<p>' + row.username + '</P><p>'+ row.role_name +'</p>';
}

var format_phone = function(value, row, index) {
  var html = '';
  if ( !empty(row.phone) ) {
    if ( row.phone_verified == 1 ) {
      html += ' <i class="fa fa-check"></i> ' + row.phone ;
    } else {
      html += '<span class="deny">'+ row.phone +'</span>' ;
    }
  }
	return html ;
}

var format_email = function(value, row, index) {
  var html = '';
  if ( !empty(row.email) ) {
    if ( row.email_verified == 1 ) {
      html += ' <i class="fa fa-check"></i> ' + row.email ;
    } else {
      html += '<span class="deny">'+ row.email +'</span>' ;
    }
  }
	return html ;
}

var opt_reset_pwd = function(value, row, index){
  return '<a href="#" data-id="'+ row.id +'" class="btn btn-info reset_pwd_btn"><i class="fa fa-lock"></i> 重置密码</a>';
}
